import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import '../../share/bassics_method.dart';
import '../../share/my_color.dart';
import '../../share/my_font.dart';
import '../../share/size_fill.dart';

class SendCodeType {
  static const String register = "USER_REGISTER_CODE"; //注册
  static const String unWeChat = "UNTYING_WECHAT_CODE"; //解绑微信
  static const String updatePassword = "UPDATE_PASSWORD_CODE"; //修改密码
  static const String updateInfo = "INFO_CHANGE_CODE"; //信息变更
  static const String authentication = "AUTHENTICATION_CODE"; //身份认证
  static const String updatePhone = "CHANGE_MOBILE_CODE"; //修改手机号
  static const String weChatLogin = "WECHAT_LOGIN_CODE"; //微信登录
  static const String login = "MOBILE_LOGIN_CODE"; //验证码登录
  static const String payPassword = "PAY_PASSWORD_CODE"; //支付密码
  static const String bindStore = "STORE_BIND_CODE";
  static const String unBindStore = "STORE_UNBIND_CODE";
}

class ShowSendCodeWidget extends StatefulWidget {
  final String? phone;
  double? fontSize;
  final String sendCodeType;
  final bool automationSend;
  final Color norTextColor;
  final Color textColor;
  final Future<bool> Function()? sendCodeOnTap;
  final Color bgColor;
  final needPhone; //需要认证手机号才能点击

  final double minWidth;
  final double height;

  ShowSendCodeWidget({
    this.phone,
    this.fontSize,
    required this.sendCodeType,
    this.automationSend = false,
    this.textColor = MyColors.color_255_72_58,
    this.norTextColor = MyColors.color_204_204_204,
    this.sendCodeOnTap,
    this.bgColor = MyColors.color_48_147_228,
    this.height = 40,
    this.minWidth = 96,
    this.needPhone = false,
  });

  @override
  _ShowSendCodeWidgetState createState() => _ShowSendCodeWidgetState();
}

class _ShowSendCodeWidgetState extends State<ShowSendCodeWidget> {
  String _title = '获取验证码';
  Timer? _timer;
  int _seconds = 60;
  bool _downTimer = false;
  bool isOnTap = true;

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (isPhoneString(widget.phone) && widget.automationSend) {
      _sendCode(context);
    }
  }

  void _sendCode(BuildContext context) async {
    if (widget.sendCodeOnTap != null) {
      widget.sendCodeOnTap!().then((value) {
        isOnTap = true;
        _downTimer = true;
        if (value != null && value) {
          _startTimer();
        } else {
          _downTimer = false;
        }
      });
    }
  }

  void _startTimer() {
    if (_timer == null || !_timer!.isActive) {
      _timer = Timer.periodic(Duration(seconds: 1), (timer) {
        if (!mounted) return;
        if (_seconds == 0) {
          _cancelTimer();
          _seconds = 60;
          _downTimer = false;
          setState(() {});
          return;
        }
        _downTimer = true;
        _seconds--;
        _title = '已发送$_seconds' + 's';
        setState(() {});
        if (_seconds == 0) {
          _title = '重新发送';
        }
      });
    }
  }

  void _cancelTimer() {
    _timer?.cancel();
  }

  @override
  Widget build(BuildContext context) {
    Color color;
    if (isPhoneString(widget.phone) && !_downTimer) {
      color = widget.textColor;
    } else {
      color = widget.norTextColor;
    }
    widget.fontSize ??= MyFont.font14;
    return ConstrainedBox(
      constraints: BoxConstraints(
        minWidth: widget.minWidth,
        maxHeight: widget.height,
        minHeight: widget.height,
      ),
      child: InkWell(
        onTap: () {
          if (!widget.needPhone && !_downTimer) {
            if (isOnTap) _sendCode(context);
          } else {
            if (isOnTap) if (!_downTimer && isPhoneString(widget.phone)) {
              _sendCode(context);
            }
          }
          isOnTap = false; //禁止点击
        },
        child: Container(
          alignment: Alignment.center,
          color: widget.bgColor,
          child: Text(
            _title,
            style: TextStyle(
              color: color,
              fontSize: widget.fontSize,
            ),
          ),
        ),
      ),
    );
    return ButtonTheme(
      minWidth: widget.minWidth,
      height: widget.height,
      padding: EdgeInsets.symmetric(horizontal: 0),
      child: FlatButton(
          splashColor: Colors.transparent,
          onPressed: () {
            if (!_downTimer && isPhoneString(widget.phone)) {
              _sendCode(context);
            }
          },
          child: Text(
            _title,
            style: TextStyle(
              color: color,
              fontSize: widget.fontSize,
            ),
          )),
    );
  }
}
